package com.gdcp.service.impl;

import com.gdcp.beans.Order;
import com.gdcp.common.ResponseModel;
import com.gdcp.mapper.OrderMapper;
import com.gdcp.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ResponseModel responseModel;

    @Override
    public int insert(Order order) {
        try{
            orderMapper.insert(order);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public List<Order> queryAllById(Integer uid) {
        return orderMapper.queryAllById(uid);
    }

    @Override
    public Map<String, Object> findPageBreakByCondition(Map<String, Object> model) {
        //bootstrap-table要求服务器返回的json须包含：totlal，rows
        Map<String,Object> result = new HashMap<>();
        int total=orderMapper.findPageBreakByConditionByShopId().size();
        List<Order> rows=orderMapper.findPageBreakByCondition(model);
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>=0) {
                model.put("a", count);
                rows = orderMapper.findPageBreakByCondition(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public Map<String,Object> removeByPrimaryKey(Integer[] ids) {
        try{
            for (Integer id:ids) {
                orderMapper.removeByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return responseModel.setModel(false,"删除["+ids.length+"条记录失败"+e.toString());
        }

        return responseModel.setModel(true,"成功删除["+ids.length+"条记录");
    }

    @Override
    public int updateInformation(Order order) {
        try{
            orderMapper.updateByPrimaryKey(order);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public Order getInformation(Integer id) {
        return orderMapper.getInformation(id);
    }

    @Override
    public Map<String,Object> searchInformation(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=orderMapper.searchInformation(null).size();
        List<Order> rows=orderMapper.searchInformation(model);
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public int deleteById(Integer oid) {
        try{
            orderMapper.removeByPrimaryKey(oid);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
